package org.lockscreen;

import java.util.ArrayList;
import java.util.List;

public class Point {
	private int x;
	private int y;

	public Point(int x, int y) {
		if (x < 1)
			throw new IllegalArgumentException("x<1");
		if (y < 1)
			throw new IllegalArgumentException("y<1");
		this.x = x;
		this.y = y;
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	@Override
	public String toString() {
		return "Point [x=" + x + ", y=" + y + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Point other = (Point) obj;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}
	
	public static List<Point> getTrail(Point p1,Point p2){
		List<Point> pointList = new ArrayList<Point>();
		getTrail( p1, p2, pointList);
		return pointList;
	}
	protected static void getTrail(Point p1,Point p2,List<Point> pointList){
		if (p1==null)
			throw new NullPointerException("p1 is null!");
		if (p2==null)
			throw new NullPointerException("p2 is null!");
		int k = Math.max(Math.abs(p1.x-p2.x), Math.abs(p1.y-p2.y));
		if(k<=1){
			return ;
		}
		if((p1.x+p2.x)%2==1){
			return ;
		}
		if((p1.y+p2.y)%2==1){
			return ;
		}
		Point p = new Point((p1.x+p2.x)/2,(p1.y+p2.y)/2);
		pointList.add(p);
		getTrail(p1,p,pointList);
		getTrail(p,p2,pointList);
	}
	
	public static void main(String[] args){
		List<Point> pointList = getTrail(new Point(1,1), new Point(5,3));
		
		for(Point p:pointList){
			System.out.println(p);
		}
		
	}
}
